package com.funong.matrix.biz.base.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.funong.matrix.biz.base.entity.SysCompanyEntity;
import com.funong.matrix.biz.base.mapper.SysCompanyMapper;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.enums.IsDeletedEnum;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

/**
 * @author wanjialin
 * @version 1.0
 * @date 2023/5/18
 */
@Dao
@Aspect
public class SysCompanyDao extends FunongBaseDao<SysCompanyMapper, SysCompanyEntity> {

    public List<SysCompanyEntity> queryCompanyDept(SysCompanyEntity sysCompanyEntity) {

        return this.baseMapper.selectList(Wrappers.<SysCompanyEntity>lambdaQuery()
                .eq(StringUtils.isNotEmpty(sysCompanyEntity.getName()), SysCompanyEntity::getName, sysCompanyEntity.getName())
                .eq(null != sysCompanyEntity.getStatus(), SysCompanyEntity::getStatus, sysCompanyEntity.getStatus())
                .eq(SysCompanyEntity::getParentId, 0)
                .eq(SysCompanyEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    public List<SysCompanyEntity> queryCompanyByParentId(Integer parentId, SysCompanyEntity condition) {
        if (condition == null) {
            condition = new SysCompanyEntity();
        }

        return this.baseMapper.selectList(Wrappers.<SysCompanyEntity>lambdaQuery()
                .eq(SysCompanyEntity::getParentId, parentId)
                .eq(null != condition.getStatus(), SysCompanyEntity::getStatus, condition.getStatus())
                .eq(SysCompanyEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .orderByAsc(SysCompanyEntity::getOrderNum)
        );
    }

    public List<SysCompanyEntity> queryCompanyByCode(String companyCode) {
        return this.baseMapper.selectList(Wrappers.<SysCompanyEntity>lambdaQuery()
                .eq(SysCompanyEntity::getCode, companyCode)
                .eq(SysCompanyEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    public List<SysCompanyEntity> queryCompanyAllByCode(String companyCode) {
        return this.baseMapper.selectList(Wrappers.<SysCompanyEntity>lambdaQuery()
                .eq(SysCompanyEntity::getCode, companyCode)
        );
    }

    public SysCompanyEntity queryCompanyDesc() {
        return this.getOne(Wrappers.<SysCompanyEntity>lambdaQuery()
                .orderByDesc(SysCompanyEntity::getId)
                .last("limit 1")
        );
    }

    public IPage<SysCompanyEntity> queryCompany(QueryDTO<SysCompanyEntity> queryDTO) {
        Page<SysCompanyEntity> page = new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize());
        ObjectMapper mapper = new ObjectMapper();
        SysCompanyEntity sysCompanyEntity = mapper.convertValue(queryDTO.getCondition(), SysCompanyEntity.class);

        return this.getBaseMapper().selectPage(page, Wrappers.<SysCompanyEntity>lambdaQuery()
                .like(StringUtils.isNotEmpty(sysCompanyEntity.getName()), SysCompanyEntity::getName, sysCompanyEntity.getName())
                .eq(null != sysCompanyEntity.getStatus(), SysCompanyEntity::getStatus, sysCompanyEntity.getStatus())
                .eq(StringUtils.isEmpty(sysCompanyEntity.getName()), SysCompanyEntity::getParentId, 0)
                .eq(SysCompanyEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .orderByAsc(SysCompanyEntity::getOrderNum)
        );
    }

}
